package com.nhim.cms.util;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nhim.cms.dao.ContentTypeDAO;
import com.nhim.cms.dao.ContentTypeElementDefinitionDAO;
import com.nhim.cms.dao.ElementDefinitionDAO;
import com.nhim.cms.domain.ContentType;
import com.nhim.cms.domain.ContentTypeElementDefinition;
import com.nhim.core.AppContext;
@Service
public class ContentTypeElementDefinitionManager {
	
	@Autowired
	private ContentTypeElementDefinitionDAO contentTypeElementDefinitionDAO;
	@Autowired
	private ElementDefinitionDAO elementDefinitionDAO;
	@Autowired
	private ContentTypeDAO contentTypeDAO;
	
	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}

	@Transactional(readOnly = true)
	public ContentTypeElementDefinition findById(Integer id) {

		return contentTypeElementDefinitionDAO.findById(id);

	}

	@Transactional(readOnly = true)
	public List<ContentTypeElementDefinition> findAll() {
		return contentTypeElementDefinitionDAO.findAll();
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void save(ContentTypeElementDefinition entity) throws Exception {
		contentTypeElementDefinitionDAO.save(entity);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean delete(Integer id) throws Exception {

		ContentTypeElementDefinition contentTypeElementDefinition = contentTypeElementDefinitionDAO.findById(id);

		if (contentTypeElementDefinition != null)
		{
			contentTypeElementDefinitionDAO.delete(contentTypeElementDefinition);
			return true;
		}
		return false;

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public ContentTypeElementDefinition saveOrUpdate(ContentTypeElementDefinition entity)
			throws Exception {
		return contentTypeElementDefinitionDAO.saveOrUpdate(entity);


		
	}
	public static ContentTypeElementDefinitionManager getInstance()
	{
		return getFromApplicationContext(AppContext.getApplicationContext());
	}

	public static ContentTypeElementDefinitionManager getFromApplicationContext(
			ApplicationContext ctx) {
		return (ContentTypeElementDefinitionManager) ctx
				.getBean(ContentTypeElementDefinitionManager.class);
	}
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean persitContentTypeElementDefinitioinsIdandPositionOnly(
			Integer contentTypeId, List<Integer> elementDefinitionIds) {
		ContentType thisContentType = contentTypeDAO.findById(contentTypeId);
		List<ContentTypeElementDefinition> persistedEntities = contentTypeElementDefinitionDAO.findByContentTypeId(contentTypeId);
		for(ContentTypeElementDefinition entity: persistedEntities)
		{
			if(!elementDefinitionIds.contains(entity.getElementDefinition().getId()))
			{
				contentTypeElementDefinitionDAO.delete(entity);
			}
		}
		for(int i= 0; i < elementDefinitionIds.size();i++)
		{
			ContentTypeElementDefinition saveCTE = contentTypeElementDefinitionDAO.findByContentTypeIdElementDefinitionId(contentTypeId,elementDefinitionIds.get(i));
			if(saveCTE != null)
			{
				saveCTE.setPosition(i+1);
				contentTypeElementDefinitionDAO.saveOrUpdate(saveCTE);
			}else
			{
				ContentTypeElementDefinition newCTE = new ContentTypeElementDefinition();
				newCTE.setPosition(i+1);
				newCTE.setElementDefinition(elementDefinitionDAO.findById(elementDefinitionIds.get(i)));
				newCTE.setContentType(thisContentType);
				newCTE.setIsRequired(false);
				newCTE.setVersion(1);
				newCTE.setDescription("");
				contentTypeElementDefinitionDAO.save(newCTE);
			}
		}
		
		return true;
		
	}

}
